package com.jscm.openselect;

import com.jscm.openprocess.OpenProcessListView;
import com.jscm.showdata.SelectDataView;
import com.jscm.showdata.TagMomeory;
import com.scm.all.export.*;
import com.scm.all.pfunc.MemoryCallBack;
import com.scm.all.struct.TagPmemory_Basic_InfoRmationX64;
import com.scm.x64.export.MemoryOperationUtilsX64;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Iterator;

/**
 * 首次搜索
 */
public class SelecthBoxSearchBtn {
    public static Button button = new Button();
    public static String dataHex;
    public static byte[] data;
    public static boolean flag = false;
    static {
        init();
    }

    private static void init() {
        button.setPrefWidth(80);
        button.setPrefHeight(40);
        button.setText("首次搜索");
        button.setId("SelectBtn");
        button.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                if(OpenProcessListView.processHandler > 0){
                    SelecthBoxSearchBtnListAddress.flag=false;
                    flag = false;
                    SelecthBoxSearchBtnListAddress.button.setText("开启列表刷新");
                    if(SelectOptionCheckBoxGame.checkBox.isSelected()){
                        ProcessAndThreadUtils.processSuspended(ProcessAndThreadUtils.pidGetProcessName(OpenProcessListView.processId),true);
                    }
                    if(SelectTextType.TypeToBytes()){
                        if(SelectTextType.comboBox.getSelectionModel().getSelectedIndex()<37 ){
                            if(ModuleOperationUtilsJNI.systemIs32()){
                                Thread thread = new Thread(new click32());
                                thread.start();
                            }
                            if(ModuleOperationUtilsJNI.systemIs64()){
                                Thread thread = new Thread(new click64());
                                thread.start();
                            }
                        }else {
                            SystemUtils.messageBoxExW(WindowOperationUtils.windowGetHwnd(),"省略号是再次搜索使用的","温馨提示",0);
                        }
                    }
                }else {
                    SystemUtils.messageBoxExW(WindowOperationUtils.windowGetHwnd(),"请先打开进程","温馨提示",0);
                }
            }
        });
    }

}
class click32 implements Runnable{

    @Override
    public void run() {


    }
}
class click64 implements Runnable{

    @Override
    public void run() {
        SelectDataView.observableList.clear();
        SelectDataView.tableView.getItems().clear();

//        判断是否为特征码 带 ？？ 这个标志 走sunday算法去匹配
        if(SelectTextType.comboBox.getSelectionModel().getSelectedIndex()==32){
            MemoryOperationUtilsX64.threadMd5SearchMemory(OpenProcessListView.processHandler,SelectText.textField.getText().toUpperCase(),new MemoryCallBack(){
                @Override
                public boolean data(TagPmemory_Basic_InfoRmationX64 tagPmemory_basic_infoRmationX64, String IsStatic, long tenAddress, String hexAddress, int memoryProtect, int memoryType) {
                    byte[] dataTemp = MemoryOperationUtilsX64.readProcessMemoryByte(OpenProcessListView.processHandler,tenAddress,ByteUtils.hexMd5ToBytesArrays(SelectText.textField.getText()).length);
                    SelecthBoxSearchBtn.dataHex = ByteUtils.bytesToHexString(dataTemp).toUpperCase();
                    SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), SelecthBoxSearchBtn.dataHex,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+SelectTextType.comboBox.getSelectionModel().getSelectedIndex(),""+dataTemp.length));
                    return false;
                }
            });
        }else {
            MemoryOperationUtilsX64.threadSearchMemory(OpenProcessListView.processHandler, SelecthBoxSearchBtn.data,2,new MemoryCallBack(){
                @Override
                public boolean data(TagPmemory_Basic_InfoRmationX64 tagPmemory_basic_infoRmationX64, byte[] data, String IsStatic, long tenAddress, String hexAddress, int memoryProtect, int memoryType) {

//                    获取数据类型选项
                    int id = SelectTextType.comboBox.getSelectionModel().getSelectedIndex();


//                    首次搜索的条件基本数据类型 根据 小于 等于 大于


                    //1-3是字节的数据类型
                    if(id >0 && id <=4){
                        char dat = ByteUtils.bytesArrayToChar(data);
                        char by = SelectText.textField.getText().charAt(0);
//                        小于当前char类型
                        if(dat < by && id==1){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat == by && id==2){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat > by && id==3){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(id==4){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                    }
                    //5-7是字节的数据类型
                    if(id >5 && id <=9){
                        short dat = ByteUtils.bytesArrayToShort1(data);
                        short by = ByteUtils.bytesArrayToShort1(ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"short"));
//                        小于当前short类型
                        if(dat <  by && id==6){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat ==  by && id==7){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat >  by && id==8){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(id==9){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                    }
                    if(id >10 && id <=14){
                        int dat = ByteUtils.bytesArrayToInt(data);
                        int by = ByteUtils.bytesArrayToInt(ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"int"));
//                        小于当前int类型
                        if(dat <  by && id==11){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat ==  by && id==12){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat >  by && id==13){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(id==14){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                    }
                    if(id >15 && id <=19){
                        long dat = ByteUtils.bytesArrayToLong1(data);
                        long by = ByteUtils.bytesArrayToLong1(ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"long"));
//                        小于当前长整型类型
                        if(dat <  by && id==16){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat ==  by && id==17){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat >  by && id==18){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(id==19){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                    }
                    if(id >20 && id <=24){
                        float dat = ByteUtils.bytesArrayToFloat1(data);
                        float by = ByteUtils.bytesArrayToFloat1(ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"float"));
//                        浮点数
                        if(dat <  by && id==21){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat ==  by && id==22){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat >  by && id==23){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(id==24){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                    }
                    if(id >25 && id <=29){
                        double dat = ByteUtils.bytesArrayToDouble1(data);
                        double by = ByteUtils.bytesArrayToDouble1(ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"double"));
//                        双精度
                        if(dat <  by && id==26){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat ==  by && id==27){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(dat >  by && id==28){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                        if(id==29){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ""+dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                    }
//                    字节数组
                    if(id >30 && id <=31){
                        byte[] dat = data;
                        byte[] by = ByteUtils.getObjectTypeToByte(ByteUtils.hexStrToBytes(SelectText.textField.getText()),"byte");
                        if(ByteUtils.equalsByteArray(dat, by)){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), ByteUtils.bytesToHexString(dat),ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                    }
                    if(id ==34){
                        String dat = ByteUtils.bytesArrayToUtf8(data);
                        String by = SelectText.textField.getText();
                        if(StringUtils.equals(dat, by)){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                    }
                    if(id ==35){
                        String dat = ByteUtils.bytesArrayToUtf8(data);
                        String by = ByteUtils.bytesArrayToUtf8(ByteUtils.getObjectTypeToByte(SelectText.textField.getText(),"StringUniCode"));
                        if(StringUtils.equals(dat, by)){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                    }
                    if(id ==36){
                        String dat = ByteUtils.bytesArrayToUtf8(data);
                        String by = SelectText.textField.getText();
                        if(StringUtils.equals(dat, by)){
                            SelectDataView.observableList.add(new TagMomeory(IsStatic,hexAddress.toUpperCase(), dat,ByteUtils.longToByteHex((tenAddress-tagPmemory_basic_infoRmationX64.BaseAddress)).toUpperCase(),""+id,""+data.length));
                        }
                    }
                    return false;
                }
            });
        }
        SelecthBoxSearchBtn.flag = true;
        if(SelectOptionCheckBoxGame.checkBox.isSelected()){
            ProcessAndThreadUtils.processSuspended(ProcessAndThreadUtils.pidGetProcessName(OpenProcessListView.processId),false);
            WindowOperationUtils.windowSetFocus(ProcessAndThreadUtils.processNameGetWindowHandle(ProcessAndThreadUtils.pidGetProcessName(OpenProcessListView.processId)));

        }
    }
}
